{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "source": [
    "# 正态随机数生成"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "标准正态分布是正态分布的一种特殊形式，即当μ=0，δ=1时，称此正态分布为标准正态分布，记为x~N（0,1）。其公式如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\varphi(x)=\\frac{1}{\\sqrt{2\\pi}}e^{-x^2/2}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 正态分布下的极大似然估计"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单变量正态分布样本的均值的最大似然估计：$$\\hat{\\mu}=\\frac{1}{N}\\sum_{k=1}^{N}x_k$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单变量正态分布样本的方差的最大似然估计：$$\\hat{\\sigma}^2=\\frac{1}{N}\\sum_{k=1}^{N}(x_k-\\hat\\mu)^2$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多变量正态分布样本的均值的最大似然估计：$$\\hat{\\mu}=\\frac{1}{N}\\sum_{k=1}^{N}x_k$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多变量正态分布样本的方差的最大似然估计：$$\\hat{\\Sigma}^2=\\frac{1}{N}\\sum_{k=1}^{N}(x_k-\\hat\\mu)(x_k-\\hat\\mu)^T$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 单个正态总体均值的区间估计"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "单个正态总体的均值的区间估计可以分为两类：方差已知和方差未知。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方差已知"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.枢轴量为 $\\frac{\\overline{x}-\\mu}{\\sigma/\\sqrt{n}\\quad}$ ~ $N(0,1)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.其置信水平为1−α的双侧置信区间为 $$(\\overline{x}-\\frac{\\sigma}{\\sqrt{n}\\quad}Z_\\frac{\\alpha}{2},\\overline{x}+\\frac{\\sigma}{\\sqrt{n}\\quad}Z_\\frac{\\alpha}{2})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.其置信水平为1−α的单侧置信下限为$$\\overline{x}-\\frac{\\sigma}{\\sqrt{n}\\quad}Z_\\alpha$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.其置信水平为1−α的单侧置信上限为$$\\overline{x}+\\frac{\\sigma}{\\sqrt{n}\\quad}Z_\\alpha$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方差未知"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.枢纽量为 $\\frac{\\overline{x}-\\mu}{s/\\sqrt{n}\\quad}$ ~ $T(n-1)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.其置信水平为1−α的双侧置信区间为 $$(\\overline{x}-\\frac{\\sigma}{\\sqrt{n}\\quad}T_\\frac{\\alpha}{2},\\overline{x}+\\frac{\\sigma}{\\sqrt{n}\\quad}T_\\frac{\\alpha}{2})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.其置信水平为1−α的单侧置信下限为$$\\overline{x}-\\frac{\\sigma}{\\sqrt{n}\\quad}T_\\alpha$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.其置信水平为1−α的单侧置信上限为$$\\overline{x}+\\frac{\\sigma}{\\sqrt{n}\\quad}T_\\alpha$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 单个正态总体方差的区间估计"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对单个正态总体方差区间估计可以分为两类：总体均值μ已知，和未知。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总体均值未知\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.枢轴量为$\\frac{(n-1)S^2}{\\sigma^2}$ ~ $X^2(n-1)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.则其置信度为1−α的双侧置信区间为$$(\\frac{(n-1)S^2}{\\chi^2_\\frac{\\alpha}{2}(n-1)},\\frac{(n-1)S^2}{\\chi^2_{1-\\frac{\\alpha}{2}}(n-1)})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.单侧置信下限为：$$\\frac{(n-1)S^2}{\\chi^2_\\alpha(n-1)}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.单侧置信上限为：$$\\frac{(n-1)S^2}{\\chi^2_{1-\\alpha}(n-1)}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总体均值已知\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.枢轴量为$\\frac{\\sum_{i=1}^{n} (X_i-\\mu)^2}{\\sigma^2}$ ~ $\\chi^2(n)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.置信度为1−α的双侧置信区间为：($$\\frac{\\sum_{i=1}^{n} (X_i-\\mu)^2}{\\chi^2_\\frac{\\alpha}{2}(n)},\\frac{\\sum_{i=1}^{n} (X_i-\\mu)^2}{\\chi^2_{1-\\frac{\\alpha}{2}}(n)}$$)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.单侧置信下限为：$$\\frac{\\sum_{i=1}^{n} (X_i-\\mu)^2}{\\chi^2_\\alpha(n)}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.单侧置信上限为：$$\\frac{\\sum_{i=1}^{n} (X_i-\\mu)^2}{\\chi^2_{1-\\alpha}(n)}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 题目：生成100万个服从标准正态分布的随机数，用最大似然估计这些随机数服从的正态分布的参数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "from scipy.stats import norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 解决图形显示中文乱码问题\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正态分布样本: [-0.5477824  -2.34789452 -0.08988861 ...  0.97303266 -0.90095841\n",
      "  1.1937192 ]\n"
     ]
    }
   ],
   "source": [
    "# 生成正态分布样本\n",
    "a = np.random.normal(loc=0, scale=1.0, size=1000000)\n",
    "print('正态分布样本:', a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本均值的无偏估计： -9.033218648887617e-05\n"
     ]
    }
   ],
   "source": [
    "# 样本均值的无偏估计\n",
    "b = np.mean(a)\n",
    "print('样本均值的无偏估计：', b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本标准差的无偏估计： 1.000408627699975\n"
     ]
    }
   ],
   "source": [
    "# 样本方差的无偏估计\n",
    "c = np.std(a, ddof=1)\n",
    "print('样本标准差的无偏估计：', c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEGCAYAAABvtY4XAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAAqz0lEQVR4nO3deXxV1bn/8c+ThDmgDBGZkiiDAkIiBGUmIEFxqIoDilqtVXp7vdpJq/1pvWq1TtXq7U9bUa51VmwrKoIQCHMIGoagKAJKQCYNFRLmkOS5f+wTCTEhO8lJ1hme9+uVV/Y5Z61zvjmB56ysvfbeoqoYY4yJPDGuAxhjjGkYVuCNMSZCWYE3xpgIZQXeGGMilBV4Y4yJUFbgTVQRkWdFpKvPtqcHvp8qImN8tI8VkYUiklTfnMYEg9gySRNuROTXwC+Ag4G7YoEmwKHA7SbAB8AvgXhgr6qqiJwAbAXaq2px4LligNaqWljpNc4HHgfOAJKBTKC/qh44Tq4LgPtUddBx2qwE4oDi4/yI3VS143EeN8YXK/Am7IlIOvBLVb2k0v1dgSUcLfzt8IrrIaAZUAAIUKKqfSv0iwFygIdU9d3AfU8DLVR1coV2Q4A3gH1ACdAL2AHsrRgj8Jq3qOoCEVkOTFTVfBEZCvweOF8D/xFFJA7IV1Vff2UYczw2RWMilqpuVdVkVT0dbyS+HbgUGAR8BZyhqqdVLO4BtwOF5cU94G5gqIg8ISISeP5lgec/A3gUWKqq3YFcYKiqpqpqiqr2VdUFgec5AhD4a+J5IAH4WET2icgvAm1KgvtOmGhlBd6EJRG5WEQu9Nk2DngFb3TdVFW3Ae8Cz4tIk0ptz8Gb2plc8X5V3QeMAcYCi0TktAp9koE/VOgzGjjen8axwPvAd8Ac4AEgC/iLn5/HGL+swJtwlQacWeH2aBFZ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cC0wHYWMByoXKiranOmj35hL7kBT9a1o00CO9okNNjzGxMODjVpztIqDtIL9v+9SPyLwE+BbwWUX3SxCOjhs02N/URkMjA5cHOfiHzhL3aj6gDsch3CsWh/D6L954coeA/k0RqbhOp7kFTdA34K/D6gRWA7nqoPjqqqTY39VHUKMMVHBmdEJLe6+a1oEe3vQbT//GDvAYTne+DnSJgVeNMrAClAvs82fvoZY4xpIH5G8NOBxSLSGRgPXCUiD6rqPcdpMxjQKu4zxhjTSGocwatqEd5O1BxgtKrmVSruVbUprOq+4EZvNCE9hdRIov09iPafH+w9gDB8D0LmQCdjjDHBZWejMsaYCGUF3gcR6Sgiq1zncEFEThCRWSKSKSLviEhT15kak4hMFZFsEbmn5taRJ9p//xWFYx2wAu/Pnzi65DPaXAM8qaoZwE7gPMd5Gk3FI7SBzoEjtKNN1P7+qxB2dcCOf6+BiIwB9uP94446qvpshZsJwLeusjiQThQcjX08Uf77/1641gEr8BWIyHNAxRNeZAFjgEvwloJGvKreA1V9QESGAG1VNcdRNBf8HMUdFaL09w9AYFrqXsKwDliBr0BVf1bxtojcCzyjqnskSs7eWPk9ABCRdsBfgGi7EoOfo7gjXhT//svdRZjWgaj8B1sLY4FbRGQBkCoiLzjO0+gCo5dpwO9UdbPrPI0s6o/GjvLff7mwrQO2Dt4nEVmgqumuczQ2Efk58EcgL3DXX1X1LYeRGo2ItAEWA/MIHI0dxgfs1Uk0//6rEm51wAq8McchIm2BDGCRqobVDjZjrMAbY0yEsjl4Y4yJUFbgjTEmQlmBN8aYCGUF3hhjIpQVeGOMiVBW4I0xJkL9H+dxBU6hQX8hAAAAAElFTkSuQmCC\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 绘制正态分布图\n",
    "plt.hist(a, density=True, bins=30)  # 归一化直方图（用出现频率代替次数），将划分区间变为 20（默认 10）\n",
    "x = np.linspace(-3, 3, 50)  # 在(-3,3)之间返回均匀间隔的50个数字。\n",
    "plt.plot(x, norm.pdf(x), 'r-')\n",
    "plt.title('正态分布图')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正态分布均值区间估计\n",
    "def confidence_interval_u(data, sigma=-1, alpha=0.05, side_both=True):\n",
    "    xb = np.mean(data)  # 均值\n",
    "    # s = np.std(data, ddof=1)  # 无偏标准差\n",
    "    # sigma已知，服从标准正态分布\n",
    "    z = stats.norm(loc=0, scale=1.0)\n",
    "    if side_both:  # 求双侧置信区间\n",
    "        tmp = sigma/np.sqrt(len(data))*z.ppf(1-alpha/2)\n",
    "        m = xb-tmp\n",
    "        n = xb+tmp\n",
    "        return m, n\n",
    "    else:  # 单侧置信下限或单侧置信上限\n",
    "        tmp = sigma/np.sqrt(len(data))*z.ppf(1-alpha)\n",
    "        m = xb-tmp\n",
    "        n = xb+tmp\n",
    "        return {'bottom_limit': m, 'top_limit': n}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正态分布标准差区间估计\n",
    "def confidence_interval_sigma(data, mu=-1, alpha=0.05, side_both=True):\n",
    "    # xb = np.mean(data)\n",
    "    # s_square = np.var(data, ddof=1)\n",
    "    sum_tmp = 0.0\n",
    "    for i in data:\n",
    "        sum_tmp = sum_tmp + (i - mu) ** 2\n",
    "    if side_both:  # 双侧置信区间\n",
    "        m = sum_tmp / stats.chi2.ppf(1 - alpha / 2, df=len(data))\n",
    "        n = sum_tmp / stats.chi2.ppf(alpha / 2, df=len(data))\n",
    "        return m**0.5, n**0.5\n",
    "    else:          # 单侧置信下限或单侧置信上限\n",
    "        return {'bottom_limit': sum_tmp / stats.chi2.ppf(1 - alpha, df=len(data)),\n",
    "                'top_limit': sum_tmp / stats.chi2.ppf(alpha, df=len(data))}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本均值的区间估计: (-0.0020502961710289302, 0.0018696317980511777)\n",
      "样本标准差的区间估计: (0.9990235943771116, 1.0017965382374272)\n"
     ]
    }
   ],
   "source": [
    "h = confidence_interval_u(a, 1)\n",
    "k = confidence_interval_sigma(a, 0)\n",
    "print('样本均值的区间估计:', h)\n",
    "print('样本标准差的区间估计:', k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}